$(function() {
	var grid;
	var url = null;
	var operation = {
		addFun : rootPath + '/dataclient/addUI.shtml',
		editFun : rootPath + '/dataclient/editUI.shtml?id=',
		delFun : rootPath + '/dataclient/deleteEntity.shtml',
		competenceFun : rootPath + '/dataclient/competenceUI.shtml?id=',
		permissionsFun : rootPath + '/dataclient/permissions.shtml?id=',
		editdropdownFun : rootPath + '/actdropdown/actdropdownList.shtml?id='
	}
	$.ajax({
		success : function(data) {
			var button = data;
			if (button !== '') {
				button = button.replace("\"", "");
				button = button.replace("\"", "");
			}
			grid = TableData({
				gaine : 'paging',//id名
				table : 'datalog',
				column : [
						{
							colkey : "enterprise",
							name : "企业",
						},
						{
							colkey : "manager",
							name : "客户姓名",
						},
						{
							colkey : "owner",
							name : "手机号码",
							render:function(data,type,hashmap,meta){
								var ids = data,datas='';
								    $.ajax({
//								    	async: false,
								        url: 'http://sj.apidata.cn/?mobile='+ids,
								        dataType: "jsonp",
								        jsonp: "callback",
								        success: function (result) {
								        	datas +='('+result.data.province+':'+result.data.city+')';
								        	//外面先执行完，再到里面，重新找到相对应的节点，然后进行修改
								        	var text=$("tr[role=row][dataid="+hashmap.id+"]").find("td[name=owner]").text();
								        	var bool = text.indexOf("(");
								        	if(bool==-1){
								        		$("tr[role=row][dataid="+hashmap.id+"]").find("td[name=owner]").append(datas);
								        	}
								        }
								    })
								return data;
							}
						},
						{
							colkey : "spec_value",
							name : "CRM套餐类型",
						},
						{
							colkey : "df_status",
							name : "套餐状态",
							render : function(data, type, hashmap, meta) {
								console.log(data+"data");
								if (data == 1) {
									return "正常";
								} else {
									return "异常";
								}
							}
						},
						{
							colkey : "status",
							name : "企业状态",
							render : function(data, type, hashmap, meta) {
								if (data == 3) {
									return "注销";
								}
								if (data == 2) {
									return "欠费";
								}
								if (data == 1) {
									return "正常";
								}
								if (data == 0) {
									return "开户失败";
								}
							}
						},
						{
							colkey : "df_create_time",
							name : "创建时间",
							render : function(data, type, row, meta) {
								function formatDate(now) {
									var y = now.getFullYear(), m = now
											.getMonth() + 1, d = now.getDate();
									//var h=now.getHours();     
									//var m=now.getMinutes();     
									//var s=now.getSeconds();     //"+h+":"+m+":"+s   
									return y + "-" + m + "-" + d;
								}
								var data = new Date(data);
								return formatDate(data);
							}
						}, {
							colkey : "operation",
							name : "操作",
							"orderable" : false,
							render : function(data, type, row, meta) {
								return button;
							}
						} ],
				url : rootPath + '/dataclient/findByPage.shtml',//表格数据请求地址
				//button: data,
				success : function() {
					//将事件绑定在body上
					//将事件绑定在body上
					dataclient = function(e) {
						var e = e || window.event;
						var target = $(e.target || e.srcElement);
						//新增
						if (target.hasClass('addFun')) {
							cover._Body();//防止多次点击
							var id = target.parent().parent().attr('dataid');
							_click(operation.addFun, 'addFun', id)
						}

						// 编辑
						if (target.hasClass('editFun')) {
							cover._Body();//防止多次点击
							var id = target.parent().parent().attr('dataid');
							_click(operation.editFun, 'editFun', id)
						}
						//  删除
						if (target.hasClass('delFun')) {
							cover._Body();//防止多次点击
							var id = target.parent().parent().attr('dataid');
							_click(operation.delFun, 'delFun', id)
						}
						// 下拉选择
						if (target.hasClass('editdropdownFun')) {
							cover._Body();//防止多次点击
							var id = target.parent().parent().attr('dataid');
							_click(operation.editdropdownFun,
									'editdropdownFun', id)
						}
					}
					$("#dataclient_wrapper").off("click", dataclient);//这个鬼东西，不知道是什么来的
					$("#dataclient_wrapper").on('click', dataclient);
					$("#dataclient_wrapper ").on("click","[name=enterprise]",function(){
						   cover._Body();//防止多次点击
					    	var id = $(this).parent().attr('dataid');
					    	var hashmap = _DataTable.row( $(this).parents('tr') ).data();
					    	var url =rootPath+ "/datacenter/clientInfoUI.shtml?owner=" + hashmap.owner +"&db_name="+ hashmap.db_name;  
						   $.get(url,function(data){
			                	layer.closeAll();
			                	cover.closeAll();// 解除多次点击限制
			                    layer.open({
			                        title : "客户详情",
			                        type : 1,
			                        area : ["650px"],
			                        content : data
			                    });
			                })
					   });
				}
			});
		}
	})
	setInterval(function(){
		$("[name=enterprise]").css({
			"color":"#0099ff",
			"cursor":"pointer"
		});
	},500)
});
